package com.example.wei.mvp.api.support;


import android.annotation.SuppressLint;
import android.support.v4.util.Preconditions;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public  final class UserAgentInterceptor implements Interceptor {
    private static final String USER_AGENT_HEADER_NAME = "User-Agent";
    private final String userAgentHeaderValue;

    @SuppressLint("RestrictedApi")
    public UserAgentInterceptor(String userAgentHeaderValue) {
        this.userAgentHeaderValue = Preconditions.checkNotNull(userAgentHeaderValue);
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        final Request originalRequest = chain.request();
        final Request requestWithUserAgent = originalRequest.newBuilder()
                .removeHeader(USER_AGENT_HEADER_NAME)
                .addHeader(USER_AGENT_HEADER_NAME, userAgentHeaderValue)
                .build();
        return chain.proceed(requestWithUserAgent);
    }

}
